{{ $scratch := newScratch }}
{{ $scratch_translated := newScratch }}
{{ $scratch_up_to_date := newScratch }}

{{ $scratch.Set "nb_pages" 0 }}
<table style="border:1px">
  <thead>
    <tr>
      <th></th>
      <th></th>
      <th>English (reference date)</th>
      {{ range sort $.Site.Languages }}
        {{ if ne .Lang "en" }}
          <th>
            {{ .Lang }}
          </th>
          {{ $scratch_translated.Set .Lang 0 }}
          {{ $scratch_up_to_date.Set .Lang 0 }}
        {{ end }}
      {{ end }}
    </tr>
  </thead>
  <tbody>
    {{ range sort $.Site.RegularPages "RelPermalink" }}
      {{ $page := . }}
      {{ if and .Translations (not .Page.Params.do_not_translate) }}
      <tr>
        <td title="{{ .File }}">
          <a href="{{ .Permalink }}">{{ .Name }}</a><br>
          {{ .RelPermalink }}
        </td>
        <td>
          <a href='https://github.com/letsencrypt/website/commits/master/content/en/{{ replace .File "\\" "/" }}'>GitHub history</a>
        </td>
        {{ $lastmod := .Lastmod }}

        <td>
          <a href="{{ .Permalink }}">
            {{ if $lastmod }}
              {{ .Lastmod.Format "January 2, 2006" }}
            {{ else }}
              {{ errorf "Missing lastmod in %q" .RelPermalink }}
            {{ end }}
          </a>
        </td>
        {{ range sort $.Site.Languages }}
            {{ if eq .Lang "en" }}
              {{ $scratch.Set "nb_pages" ( add 1 ( $scratch.Get "nb_pages" ) ) }}
            {{ else }}
              {{ $lang := . }}
            <td data-lang="{{ $lang }}">
              {{ $scratch.Set "with_page" 0 }}
              {{ range $page.Translations }}
                {{ if eq $lang .Language }}
                
                {{ if .Params.aliases }}
                  {{ errorf "Translated page %q must not have aliases" $page.RelPermalink }}
                {{ end }}

                <a href="{{ .Permalink }}">
                  {{ $scratch.Set "with_page" 1 }}
                  {{ if .Page.Params.untranslated }}
                    Not yet translated
                  {{ else if  $lastmod }}
                    {{ $scratch_translated.Set .Lang ( add 1 ( $scratch_translated.Get .Lang ) ) }}
                    {{ if not .Lastmod }}
                      {{ errorf "Missing lastmod in %q" .RelPermalink }}
                    {{ else if ne  .Lastmod $lastmod }}
                        <span style="color:orange">{{ .Lastmod.Format "January 2, 2006" }}</span>
                    {{ else }}
                      {{ $scratch_up_to_date.Set .Lang ( add 1 ( $scratch_up_to_date.Get .Lang ) ) }}
                        <span style="color:grey">{{ .Lastmod.Format "January 2, 2006" }}</span>
                    {{ end }}
                  {{ end }}
                </a>
                {{ end }}
              {{ end }}
              {{ if not ( $scratch.Get "with_page") }}
                {{ errorf "Missing templatte placeholder for page %q in %q" $page.Path $lang }}
              {{ end }}
            </td>
            {{ end }}
        {{ end }}
      </tr>
      {{ end }}
    {{ end }}
  </tbody>
  <tfoot>
      <tr>
        <th></th>
        <th></th>
        <th></th>
        {{ range sort $.Site.Languages }}
          {{ if ne .Lang "en" }}
            <th>
              {{ .Lang }}
            </th>
            {{ end }}
          {{ end }}
      </tr>
      <tr>
        <th></th>
        <th></th>
        <th>Translated</th>
        {{ range sort $.Site.Languages }}
          {{ if ne .Lang "en" }}
            <th>
                {{ $scratch_translated.Get .Lang }}/{{ $scratch.Get "nb_pages" }}
                ({{ math.Round (mul 100 (div ($scratch_translated.Get .Lang) (mul 1.0 ($scratch.Get "nb_pages")))) }}%)
            </th>
            {{ end }}
          {{ end }}
      </tr>
      <tr>
        <th></th>
        <th></th>
        <th>Up-to-date</th>
        {{ range sort $.Site.Languages }}
          {{ if ne .Lang "en" }}
            <th>
              {{ if ($scratch_translated.Get .Lang) }}
                {{ $scratch_up_to_date.Get .Lang }}/{{ $scratch_translated.Get .Lang }}
                ({{ math.Round (mul 100 (div ($scratch_up_to_date.Get .Lang) (mul 1.0 ($scratch_translated.Get .Lang)))) }}%)
              {{ end }}
            </th>
            {{ end }}
          {{ end }}
      </tr>
  </tfoot>
</table>
